#include <Windows.h>
#include "FileManager.h"

FileManager::FileManager()
{	
}

FileManager::~FileManager()
{	
}

#ifdef _UNICODE
std::vector< File > FileManager::listFiles( std::wstring folder )
#else
std::vector< File > FileManager::listFiles( std::string folder )
#endif
{
	std::vector< File > list;

	WIN32_FIND_DATA FindFileData;
	HANDLE hFind;

#ifdef _UNICODE
	std::wstring tfold = folder;
#else
	std::string tfold = folder;
#endif

	tfold += TEXT( "\\*.*" );
		
	hFind = FindFirstFile( tfold.c_str(), &FindFileData );
				
	while ( hFind != INVALID_HANDLE_VALUE ) 
	{
		File file;
		file.name = FindFileData.cFileName;
		file.directory = ( FILE_ATTRIBUTE_DIRECTORY & FindFileData.dwFileAttributes ) ? true : false;

		if ( file.name != TEXT( "." ) && file.name != TEXT( ".." ) )
		{
			list.push_back( file );
		}
		
		if ( !FindNextFile( hFind, &FindFileData ) ) break;
	}
	FindClose( hFind );

	return list;
}

#ifdef _UNICODE
std::wstring FileManager::lastPathName( std::wstring path )
#else
std::string FileManager::lastPathName( std::string path )
#endif
{
	size_t pos = path.find_last_of( TEXT( "/\\" ) );
	return ( pos < path.length() ) ? path.substr( pos + 1 ) : path;
}

#ifdef _UNICODE
std::wstring FileManager::fileExtention( std::wstring path )
#else
std::string FileManager::fileExtention( std::string path )
#endif
{
	size_t pos = path.find_last_of( TEXT( "." ) );
	return path.substr( pos + 1 );
}

#ifdef _UNICODE
std::wstring filePath( std::wstring path )
#else
std::string filePath( std::string path )
#endif
{
	size_t pos = path.find_last_of( TEXT( "/\\" ) );
	return ( pos < path.length() ) ? path.substr( 0, pos ) : path;
}